package com.campusflea;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImgActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager = null;
    private LinearLayout mViewGroup = null;

    private int[] mImageIds = {R.mipmap.c1,R.mipmap.c2,
            R.mipmap.c3,R.mipmap.c4,
            R.mipmap.c5,R.mipmap.c6,
            R.mipmap.c7};

    private ImageView[] mImageViews = null;

    private ImageView[] mTips = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mViewGroup = (LinearLayout) findViewById(R.id.viewGroup);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);

        mTips = new ImageView[mImageIds.length];
        //动态创建小点并加到布局中
        for(int i = 0; i < mTips.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new ViewPager.LayoutParams());
            mTips[i] = iv;

            if(i == 0)
            {
                iv.setBackgroundResource(R.mipmap.xuanzhong);
            }else
            {
                iv.setBackgroundResource(R.mipmap.noxuanzhong);
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, ViewPager.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            mViewGroup.addView(iv,lp);
        }
        mImageViews = new ImageView[mImageIds.length];
        for(int i = 0; i < mImageViews.length; i++)
        {
            ImageView iv = new ImageView(this);
            mImageViews[i] = iv;
            int reqWidth = getWindowManager().getDefaultDisplay().getWidth();
            int reqHeight = getWindowManager().getDefaultDisplay().getHeight();
            iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mImageIds[i], reqWidth, reqHeight));
        }

        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.setOnPageChangeListener(this);

    }

    class MyPagerAdapter extends PagerAdapter
    {
        @Override
        public int getCount()
        {
            return mImageIds.length;
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == arg1;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            try
            {
                container.addView(mImageViews[position]);
            } catch (Exception e)
            {
            }
            return mImageViews[position];
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
        }
    }
    @Override
    public void onPageSelected(int arg0)
    {
        for(int i = 0; i < mTips.length; i++)
        {
            if(arg0 == i)
            {
                mTips[i].setBackgroundResource(R.mipmap.xuanzhong);
            }else
            {
                mTips[i].setBackgroundResource(R.mipmap.noxuanzhong);
            }
        }
    }
    @Override
    public void onPageScrollStateChanged(int arg0)
    {
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
    }

    private static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
    {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId);
        int inSampleSize = cacluateInSampledSize(opts, reqWidth, reqHeight);
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res,resId,opts);
    }

    private static int cacluateInSampledSize(BitmapFactory.Options opts,int width,int height)
    {
        if(opts == null)
        {
            return 1;
        }
        int inSampleSize = 1;
        int realWidth = opts.outWidth;
        int realHeight = opts.outHeight;

        if(realWidth > width || realHeight > height)
        {
            int heightRatio = realHeight/height;
            int widthRatio = realWidth/width;

            inSampleSize = (widthRatio > heightRatio) ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }
}
